/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.tsdemo.web.rulemodule;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fosung.frame.common.config.Global;
import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.tsdemo.entity.rulemodule.RuleModule;
import com.fosung.frame.tsdemo.service.rulemodule.RuleModuleService;

/**
 * 用户行业层级结构Controller
 * @author zhaofj
 * @version 2017-07-06
 */
@Controller
@RequestMapping(value = "${adminPath}/tsdemo/rulemodule/ruleModule")
public class RuleModuleController extends BaseController {

	@Autowired
	private RuleModuleService ruleModuleService;
	
	@ModelAttribute
	public RuleModule get(@RequestParam(required=false) String id) {
		RuleModule entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = ruleModuleService.get(id);
		}
		if (entity == null){
			entity = new RuleModule();
		}
		return entity;
	}
	
	@RequiresPermissions("tsdemo:rulemodule:ruleModule:view")
	@RequestMapping(value = {"list", ""})
	public String list(RuleModule ruleModule, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<RuleModule> page = ruleModuleService.findPage(new Page<RuleModule>(request, response), ruleModule); 
		model.addAttribute("page", page);
		return "frame/tsdemo/rulemodule/ruleModuleList";
	}

	@RequiresPermissions("tsdemo:rulemodule:ruleModule:view")
	@RequestMapping(value = "form")
	public String form(RuleModule ruleModule, Model model) {
		//行业
		String modelIds=ruleModule.getModelIds();
		if(modelIds!=null&&!modelIds.equals("")){
			List<String> result = Arrays.asList(modelIds.split(",")); 
			ruleModule.setModelIdsList(result);
		}
		//层级
		String levelIds=ruleModule.getLevelIds();
		if(levelIds!=null&&!levelIds.equals("")){
			List<String> result = Arrays.asList(levelIds.split(",")); 
			ruleModule.setLevelIdsList(result);
		}
		
		model.addAttribute("ruleModule", ruleModule);
		return "frame/tsdemo/rulemodule/ruleModuleForm";
	}

	@RequiresPermissions("tsdemo:rulemodule:ruleModule:edit")
	@RequestMapping(value = "save")
	public String save(RuleModule ruleModule, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, ruleModule)){
			return form(ruleModule, model);
		}
		//行业
		List<String> modelIdsList=ruleModule.getModelIdsList();
		if(modelIdsList.size()!=0){
			String str = StringUtils.join(modelIdsList.toArray(), ","); 
			ruleModule.setModelIds(str);
		}
		//层级
		List<String> levelIdsList=ruleModule.getLevelIdsList();
		if(levelIdsList.size()!=0){
			String str = StringUtils.join(levelIdsList.toArray(), ","); 
			ruleModule.setLevelIds(str);
		}
		ruleModuleService.save(ruleModule);
		addMessage(redirectAttributes, "保存用户行业层级结构成功");
		return "redirect:"+Global.getAdminPath()+"/tsdemo/rulemodule/ruleModule/?repage";
	}
	
	@RequiresPermissions("tsdemo:rulemodule:ruleModule:edit")
	@RequestMapping(value = "delete")
	public String delete(RuleModule ruleModule, RedirectAttributes redirectAttributes) {
		ruleModuleService.delete(ruleModule);
		addMessage(redirectAttributes, "删除用户行业层级结构成功");
		return "redirect:"+Global.getAdminPath()+"/tsdemo/rulemodule/ruleModule/?repage";
	}

}